import java.util.Random;
public class WeightCollection {
	private int[] weights; //collection of weights
	private int boundMax = 100;
	private int boundMin = 1;
	
	//PRELIMINARY Table of weights - subject to change
	// 0 - Score differential
	// 1 - difference in control
	// 2 - number of stones to capture
	// 3 - number of stones to be captured
	// 4 - free move
	// 5 - ???
	// Change numWeights to add/remove weights.
	
	private static int numWeights = 5;
	
	public static WeightCollection[] mingle(WeightCollection col1, WeightCollection col2, int mutationRate)
	{
		Random random = new Random();
		int[] newCol1 = new int[numWeights];
		int[] newCol2 = new int[numWeights];
		WeightCollection[] pair = new WeightCollection[2]; //need two to keep population stable
		
		int crossover = random.nextInt(numWeights-1) + 1; //where we want to crossover, -1 to exclude zero since then nothing happens	
		for(int i = 0; i < crossover; i++) //goes until crossover point, then switches between the two
		{
			newCol1[i] = col1.getWeight(i);
			newCol2[i] = col2.getWeight(i);
		}
		
		for(int i = crossover; i < numWeights; i++) //start at crossover point, go until the end
		{
			newCol1[i] = col2.getWeight(i);
			newCol2[i] = col1.getWeight(i);
		}
		
		
		if(random.nextInt(100) < mutationRate) //if true, mutation will occur
		{
			int weightA = random.nextInt(numWeights); //weight to be subtracted from
			int weightB = random.nextInt(numWeights); //weight to be added too
			
			while(weightA == weightB) //makes sure we are not dealing with the same weight
			{
				weightB = random.nextInt(numWeights);
						
			}
			
			int difference = random.nextInt(3)+1;
			
			newCol1[weightA] = newCol1[weightA] - difference;
			newCol1[weightB] = newCol1[weightB] + difference;
		} 
		
		if(random.nextInt(100) < mutationRate) //if true, mutation will occur
		{
			int weightA = random.nextInt(numWeights); //weight to be subtracted from
			int weightB = random.nextInt(numWeights); //weight to be added too
			
			while(weightA == weightB) //makes sure we are not dealing with the same weight
			{
				weightB = random.nextInt(numWeights);
						
			}
			
			int difference = random.nextInt(3)+1;
			
			newCol2[weightA] = newCol2[weightA] - difference;
			newCol2[weightB] = newCol2[weightB] + difference;
		} 
		
		pair[0] = new WeightCollection(newCol1);
		pair[1] = new WeightCollection(newCol2);
		
		return pair;
	}
	public WeightCollection()
	{
		weights = new int[numWeights];
		Random random = new Random();
		for(int i = 0; i < weights.length; i++)
		{
			weights[i] = random.nextInt(boundMax - boundMin) + boundMin;
		}
	}
	
	public WeightCollection(int[] weights)
	{
		this.weights = weights;
	}

	public int getWeight(int index)
	{
		return weights[index];
	}
	
	public int[] getAllWeights()
	{
		return weights;
	}
}